// 2023年6月28日 执行计划序时表表格右键菜单
const URL_FINISH = 'execPlanMain/finisheda';// 执行计划完工API
const URL_CANCEL_FINISH = 'execPlanMain/finished/cancels'; // 取消选中行的完工状态
const GRID_ID = 'v_grid';

// 初始化右键菜单
$(document).bind('contextmenu', function (e) {
    e.preventDefault();
    $('#gridMenu').menu('show', {
        left: e.pageX,
        top: e.pageY
    });
});

// 刷新表格数据
function refreshGrid(){
    $(`#${GRID_ID}`).data("kendoGrid").dataSource.read({
        start: $('#start').datebox('getValue'),
        end: $('#end').datebox('getValue'),
        isFinished: $('#type').combobox('getValue') > 1 ? null : $('#type').combobox('getValue')
    });
}

function getSelectedIds(){
    const selectedRows = getKendoGridSelectedRowData(GRID_ID);
    return selectedRows.map(item => item.id)
}

function finisheRows(rkDate, callback){
    // const selectedRows = getKendoGridSelectedRowData(GRID_ID);
    // const ids = selectedRows.map(item => item.id)
    const ids = getSelectedIds();
    const params = {"idList":ids,"rkDate":rkDate};
    console.log('请求完工并入库发送的参数：', params);
    $.messager.progress({title: '正在提交...', interval: 500});
    $.ajax({
        url: URL_FINISH,
        contentType: 'application/json',
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(params),
        success: function (res) {
            if (res.success || res.code === 1 ) {
                refreshGrid();
                if(callback) callback();
            } else {
                $.messager.alert('提示', res.message, 'warning');
            }
        },
        complete: function () {
            $.messager.progress('close');
        }
    })
}

// 取消完工
function cancelFinishe(){
    $.messager.confirm('提示', '您确认要取消选中行的完工状态吗?', function (r) {
        if(!r) return;

        const ids = getSelectedIds();
        $.messager.progress({title: '正在提交...', interval: 500});
        $.ajax({
            url: URL_CANCEL_FINISH,
            contentType: 'application/json',
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(ids),
            success: function (res) {
                if (res.success || res.code === 1 ) refreshGrid();
                else $.messager.alert('提示', res.message, 'warning');
            },
            complete: function () {
                $.messager.progress('close');
            }
        })
    });
}